package com.fsh.subject.domain.handler.subject;

import com.fsh.subject.common.enums.IsDeletedFlagEnum;
import com.fsh.subject.common.enums.SubjectInfoTypeEnum;
import com.fsh.subject.domain.convert.RadioSubjectConverter;
import com.fsh.subject.domain.entity.SubjectAnswerBO;
import com.fsh.subject.domain.entity.SubjectInfoBO;
import com.fsh.subject.domain.entity.SubjectOptionBO;
import com.fsh.subject.infra.basic.entity.SubjectRadio;
import com.fsh.subject.infra.basic.service.SubjectRadioService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选题目的策略类。   实现类
 *
 * @author: ChickenWing
 * @date: 2023/10/5
 */
@Component
@Slf4j
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService radioService;

    /**
     * 枚举身份的识别 。 返回题目类别的 枚举
     */
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;//返回  单选的枚举
    }

    /**
     * 单选题 选项的插入
     */
    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        Preconditions.checkNotNull(subjectInfoBO.getOptionList(),"选项不能为空");
        Preconditions.checkArgument(!subjectInfoBO.getOptionList().isEmpty(),"没有对应的选项");

        List<SubjectRadio> subjectRadioList=new LinkedList<>();
        subjectInfoBO.getOptionList().forEach(option->{
            //转换
            SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE.convertBOToRadio(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
           subjectRadioList.add(subjectRadio);
        });
        //批量插入选项
        radioService.batchInsert(subjectRadioList);
    }

    /**
     * 实际的单选题目的查询
     */
    @Override
    public SubjectOptionBO query(Long subjectId) {
        //查询radio表
        SubjectRadio subjectRadio=new SubjectRadio();
        subjectRadio.setSubjectId(subjectId);
        subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectRadio> subjectRadioList = radioService.queryByCondition(subjectRadio);
        //转换
        List<SubjectAnswerBO> subjectAnswerBOS = RadioSubjectConverter.INSTANCE.convertRadioToAnswerBOList(subjectRadioList);
        //赋值
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
